﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Silmoon.Threading;
using Cooky.Client.DomainDir;
using Silmoon.Configure;

namespace Cooky.Client
{
    public partial class ClientFaceForm : Silmoon.Windows.Forms.GenieForm
    {
        cklib.CookyDNSAPI cooky = new cklib.CookyDNSAPI();
        public ClientFaceForm()
        {
            InitializeComponent();
        }
        public void ShowLogin()
        {
            IniFile ini = new IniFile(Application.StartupPath + "/conf");
            string apiurl = ini.ReadInivalue("config", "apiurl");

            if (string.IsNullOrEmpty(apiurl))
            {
                MessageBox.Show("API 地址没有设定。");
                Application.Exit();
                return;
            }
            cooky.SetAPIUrl(apiurl);
            Show();
            Visible = false;
            LoginForm lf = new LoginForm(cooky);
            lf.FormClosed += new FormClosedEventHandler(lf_FormClosed);
            lf.ShowEx();
        }

        void lf_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoginForm lf = ((LoginForm)sender);
            if (cooky.IsLogin)
            {
                label1.Text = "已经登录：" + cooky.Username;
                ShowEx();
                ctlClientButton_Click(this, EventArgs.Empty);
            }
            else
                Close();
        }
        void hideThis()
        {
            panel1.Visible = false;
            HideEx();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            panel1.Visible = false;
            base.OnFormClosing(e);
        }

        private void ctlExitButton_Click(object sender, EventArgs e)
        {
            hideThis();
            Close();
        }

        private void ctlClientButton_Click(object sender, EventArgs e)
        {
            hideThis();
            DomainListForm domainListForm = new DomainListForm(cooky);
            domainListForm.FormClosed += new FormClosedEventHandler(domainListForm_FormClosed);
            domainListForm.ShowEx();
        }

        void domainListForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Close();
        }
    }
}
